package com.zys.http.ui.tab.panel;

import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.util.ui.JBUI;
import com.zys.http.constant.HttpEnum;
import com.zys.http.constant.UIConstant;
import com.zys.http.entity.ReqHistory;
import com.zys.http.entity.param.ParamProperty;
import com.zys.http.extension.service.Bundle;
import com.zys.http.ui.table.FileUploadTable;
import jdk.jfr.Description;
import lombok.Getter;

import javax.swing.*;
import java.awt.*;
import java.util.Map;

/**
 * @author zys
 * @since 2025-02-19
 */
@Getter
public class RequestFilePanel extends JBPanel<RequestFilePanel> implements TabOperation {
    @Description("文件上传部分名")
    private final JTextField partTextField = new JTextField();
    @Description("文件上传选择器")
    private transient final FileUploadTable fileUploadTable;

    public RequestFilePanel(Project project) {
        super(new BorderLayout(0, 0));
        JPanel partNamePanel = new JPanel(new BorderLayout(0, 0));
        JLabel label = new JLabel(Bundle.get("http.api.tab.file.label") + " ");
        partNamePanel.add(label, BorderLayout.WEST);

        partNamePanel.add(this.partTextField, BorderLayout.CENTER);
        this.add(partNamePanel, BorderLayout.NORTH);

        this.fileUploadTable = new FileUploadTable(project);
        this.fileUploadTable.getToolbar().getComponent().setBorder(JBUI.Borders.customLine(UIConstant.BORDER_COLOR, 1, 0, 1, 0));
        this.add(this.fileUploadTable, BorderLayout.CENTER);
    }

    @Override
    public void reset() {
        this.partTextField.setText("");
        this.fileUploadTable.reloadTableModel();
    }

    @Override
    public void execute(HttpEnum.HttpMethod httpMethod, HttpEnum.ContentType contentType, String param, Map<String, ParamProperty> paramPropertyMap) {
        ParamProperty v = paramPropertyMap.get(param);
        HttpEnum.ParamUsage usage = v.getParamUsage();
        if (HttpEnum.ParamUsage.FILE.equals(usage)) {
            this.partTextField.setText(param);
        }
    }

    @Override
    public void recover(ReqHistory history) {
        this.fileUploadTable.setModel(history.getFileNames());
    }
}
